﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CompanyBussiness;

namespace Website.customer.Control
{
    public partial class cListProductByCategory : System.Web.UI.UserControl
    {
        HtmlMeta metaTag;
        NewsManagement newsMana = new NewsManagement();
        ProductManagement proMana = new ProductManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        TurnOverService overServices = new TurnOverService();
        CompanyBussiness.Product pro = new Product();
        CompanyBussiness.Language language;
        CheckValid cv = new CheckValid();
        public int CurPage = 1;
        int TotalRow;
        int TotalPage;
        int StartRow = 0;
        int typeProductIDs;
        string modulCode;
        Metadata meta;
        bool searchWithName = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            modulCode = Request.QueryString["Mod"];
            loadModul();

            string abc = Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString();
            typeProductIDs = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString());

            if (Request.Cookies[CONST.COOKIES_LANGUAGE] == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }

            if (!Page.IsPostBack)
            {
                if (Request[CONST.QUERYSTRING_TYPE_PRODUCT] == null && Request[CONST.QUERYSTRING_NAME_PRODUCT] == null)
                {
                    //Lay tat ca san pham
                }
                else if (Request[CONST.QUERYSTRING_TYPE_PRODUCT] != null)
                {
                    typeProductIDs = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString());
                    lbNameCategory.Text = proMana.GetTypeProductByID(typeProductIDs) != null ? proMana.GetTypeProductByID(typeProductIDs).TypeProductName : "";
                    this.LoadDataList(int.Parse(dropRows.SelectedValue));

                    meta = newsMana.GetMetadataByTypeProductID(typeProductIDs);
                    if (meta != null)
                    {
                        //ghi metadata
                        metaTag = new HtmlMeta();
                        metaTag.Name = "keywords";
                        metaTag.Content = meta.MetaKeyword;
                        Page.Header.Controls.Add(metaTag);

                        metaTag = new HtmlMeta();
                        metaTag.Name = "description";
                        metaTag.Content = meta.MetaDesciption;
                        Page.Header.Controls.Add(metaTag);

                        Page.Header.Title = meta.Title;
                    }
                }
                else if (Request[CONST.QUERYSTRING_NAME_PRODUCT] != null)
                {
                    lbNameCategory.Text = "";
                    searchWithName = true;
                }
            }
        }

        protected void loadModul()
        {
            switch (modulCode)
            {
                case "MNEWS":
                    {

                    }
                    break;
                case "MSHAREHOLDERS":
                    {

                    }
                    break;
                case "MJOB":
                    {

                    }
                    break;
                case "MHELP":
                    {

                    }
                    break;
                case "MCONTACT":
                    {

                    }
                    break;
                case "MINTRO":
                    {

                    }
                    break;
                case "MPRODUCT":
                    {

                    }
                    break;
            }


        }

        protected void LoadDataList(int take)
        {

            try
            {
                typeProductIDs = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString());

                if (Request["Page"] == null)
                {
                    CurPage = 1;
                }
                else
                {
                    CurPage = Convert.ToInt32(Request["Page"]);
                }

                dropChoicePage.SelectedValue = CurPage.ToString();

                if (CurPage > 1)
                {
                    StartRow = (CurPage - 1) * 10;
                }
                else
                {
                    StartRow = 0;
                }

                if (searchWithName == true)
                {
                    dataListProduct.DataSource = proMana.GetProductByName(Request[CONST.QUERYSTRING_NAME_PRODUCT].ToString(), take, StartRow);
                    dataListProduct.DataBind();
                    TotalRow = proMana.GetProductByName(Request[CONST.QUERYSTRING_NAME_PRODUCT].ToString(), take, StartRow).Count();
                }
                else
                {
                    dataListProduct.DataSource = proMana.GetProductByTypeProduct(typeProductIDs, take, StartRow);
                    dataListProduct.DataBind();
                    TotalRow = proMana.GetProductByTypeProduct(typeProductIDs, take, StartRow).Count();
                }




                if (TotalRow == 0)
                {
                    lbNameCategory.Text = languageMana.GetTagValueByLanguageIDandTagName("NotfoundNews", language.LanguagesID);
                    pnPagination.Visible = false;
                    return;
                }
                else
                {
                    pnPagination.Visible = true;
                }

                this.Pagination(take);
            }
            catch (Exception)
            {

            }
        }

        protected void Pagination(int take)
        {
            try
            {
                typeProductIDs = int.Parse(Request[CONST.QUERYSTRING_TYPE_PRODUCT].ToString());

                if (TotalRow % take == 0)
                    TotalPage = TotalRow / take;
                else
                    TotalPage = (TotalRow / take) + 1;

                dropChoicePage.Items.Clear();
                for (int i = 0; i < TotalPage; i++)
                {
                    dropChoicePage.Items.Add((i + 1).ToString());
                }


                if (CurPage > 1)
                {
                    //lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?"  + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(1)+ "&Mod=MPRODUCT";
                    lnkFirst.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + 1 + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";
                    //lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(CurPage - 1) + "&Mod=MPRODUCT";
                    lnkPrev.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + (CurPage - 1) + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";
                }
                if (CurPage < TotalPage && CurPage > 1)
                {
                    //lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(TotalPage.ToString()) + "&Mod=MPRODUCT";
                    lnkLast.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + TotalPage.ToString() + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";
                    //lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(CurPage + 1) + "&Mod=MPRODUCT";
                    lnkNext.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + (CurPage + 1) + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";
                }


                //numCurrent.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(CurPage) + "&Mod=MPRODUCT";
                numCurrent.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + CurPage + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";
                //numBefore.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(CurPage - 1) + "&Mod=MPRODUCT";
                numBefore.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + (CurPage - 1) + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";
                //numAfter.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(CurPage + 1) + "&Mod=MPRODUCT";
                numAfter.NavigateUrl = "~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + (CurPage + 1) + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "-")) + ".html";

                if (CurPage == 1 && TotalPage == 1)
                {
                    numCurrent.Text = CurPage.ToString();
                    numBefore.Text = "";
                    numAfter.Text = "";
                    numBefore.Visible = false;
                    numAfter.Visible = false;
                }
                else if (CurPage == 1 && TotalPage > 1)
                {
                    numCurrent.Text = CurPage.ToString();
                    numBefore.Text = "";
                    numBefore.Visible = false;
                    numAfter.Text = Convert.ToString(CurPage + 1);
                }
                else if (1 < CurPage && CurPage < TotalPage)
                {
                    numCurrent.Text = CurPage.ToString();
                    numBefore.Text = Convert.ToString(CurPage - 1);
                    numAfter.Text = Convert.ToString(CurPage + 1);
                }
                else if (CurPage == TotalPage)
                {
                    numCurrent.Text = CurPage.ToString();
                    numBefore.Text = Convert.ToString(CurPage - 1);
                    numAfter.Text = "";
                    numAfter.Visible = false;
                }
            }
            catch (Exception)
            {

            }
        }

        protected void dropChoicePage_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurPage = Convert.ToInt32(dropChoicePage.SelectedValue.ToString());
            //Response.Redirect(Request.CurrentExecutionFilePath + "?" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + typeProductIDs + "&Page=" + Convert.ToString(CurPage) + "&Mod=MPRODUCT");
            Response.Redirect("~/Hem/san-pham-dich-vu/" + typeProductIDs + "_" + "MPRODUCT" + "_" + CurPage + "_" + overServices.ConvertString(proMana.GetNametypeByID(typeProductIDs).Replace(" ", "_")) + ".html");
        }

        protected void dropRows_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                this.LoadDataList(int.Parse(dropRows.SelectedValue));
            }
            catch (Exception)
            {

            }
        }

        protected void imgProduct_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                Session[CONST.SESSION_PRODUCT_ID] = Convert.ToInt32(e.CommandArgument);
                try
                {
                    //Response.Redirect("Products.aspx?" + CONST.QUERYSTRING_PRODUCT + "=" + Session[CONST.SESSION_PRODUCT_ID].ToString() + "&Mod=MPRODUCT");
                    Response.Redirect("~/Hem/" + Session[CONST.SESSION_PRODUCT_ID].ToString() + "_" + typeProductIDs + "/San-pham-dich-vu/" + "MPRODUCT" + "/" + overServices.ConvertString(proMana.GetNameProductByID(int.Parse(Session[CONST.SESSION_PRODUCT_ID].ToString())).Replace(" ", "-")) + ".html");
                }
                catch (Exception ex)
                {

                }
            }
        }

        protected void lnkDetailProduct_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                Session[CONST.SESSION_PRODUCT_ID] = Convert.ToInt32(e.CommandArgument);
                try
                {
                    //Response.Redirect("Products.aspx?" + CONST.QUERYSTRING_PRODUCT + "=" + Session[CONST.SESSION_PRODUCT_ID].ToString() + "&Mod=MPRODUCT");
                    Response.Redirect("~/Hem/" + Session[CONST.SESSION_PRODUCT_ID].ToString() + "_" + typeProductIDs + "/San-pham-dich-vu/" + "MPRODUCT" + "/" + overServices.ConvertString(proMana.GetNameProductByID(int.Parse(Session[CONST.SESSION_PRODUCT_ID].ToString())).Replace(" ", "-")) + ".html");
                }
                catch (Exception)
                {

                }
            }
        }

        protected void dataListProduct_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
            {


                ImageButton imgButton = e.Item.FindControl("imgProduct") as ImageButton;

                if (imgButton.ImageUrl == null || imgButton.ImageUrl.ToString() == string.Empty)
                {
                    imgButton.Visible = true;
                }

            }
        }
    }
}